package org.cloudlab.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.cloudlab.bean.AcademicJournal;
import org.cloudlab.bean.JournalVO;
import org.cloudlab.bean.SysUser;
import org.cloudlab.common.RemoveState;
import org.cloudlab.common.ResponseResult;
import org.cloudlab.service.IAcademicJournalService;
import org.cloudlab.utils.JwtTokenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 期刊页面的Controller
 *
 * @author Liu Yicong
 * @date 2023/9/4
 */
@CrossOrigin // 允许跨域访问
@RestController
@RequestMapping(value = "journal")
public class AcademicJournalController {

	private static final Logger logger = LoggerFactory.getLogger(AcademicJournalController.class);

	@Autowired
	private IAcademicJournalService journalService;

	@Autowired
	private JwtTokenUtil tokenUtil;

	/**
	 * 查询页面
	 */
	@RequestMapping("queryByPage")
	public ResponseResult<Page<AcademicJournal>> queryByPage(Page<AcademicJournal> page, AcademicJournal journal,
															 String sortOrder) {
		Page<AcademicJournal> list = journalService.queryByPage(page, journal, sortOrder);
		return ResponseResult.success("查询成功", list);
	}

	/**
	 * 查询所有期刊数据(下拉框)
	 */
	@GetMapping("queryAll")
	public ResponseResult<List<JournalVO>> queryAll() {
		List<AcademicJournal> list = journalService.lambdaQuery()
				.eq(AcademicJournal::getIsRemove, RemoveState.NOT_REMOVE.getValue()).list();
		List<JournalVO> result = new ArrayList<>();
		list.forEach(journal -> {
			JournalVO journalVO = new JournalVO(journal.getId(), journal.getJournalName());
			result.add(journalVO);
		});
		return ResponseResult.success("查询成功", result);
	}


	/**
	 * 添加期刊
	 *
	 * @param journal 期刊属性
	 */
	@PostMapping("add")
	public ResponseResult<String> add(@RequestBody AcademicJournal journal) {
		try {
			journalService.add(journal);
		} catch (Exception e) {
			logger.error("Exception=======>", e);
			return ResponseResult.fail(e.getMessage(), null);
		}
		return ResponseResult.success();
	}

	/**
	 * 更新
	 *
	 * @param journal 期刊信息, 其中id为必须的
	 */
	@PostMapping("update")
	public ResponseResult<String> update(@RequestBody AcademicJournal journal) {
		try {
			SysUser currentUser = tokenUtil.getCurrentUser();
//			if (!Objects.equals(currentUser.getId(), CommonConst.ADMIN_ID)) {
//				return ResponseResult.fail("你没有更新期刊信息的权限哦, 如果需要更新, 请使用管理员账户", null);
//			}
			journal.setUpdateUserId(currentUser.getId());
			journal.setUpdateTime(new Date());
			journalService.updateById(journal);
		} catch (Exception e) {
			logger.error("Exception=======>", e);
			return ResponseResult.fail(e.getMessage(), null);
		}
		return ResponseResult.success();
	}

	/**
	 * 删除
	 *
	 * @param journalId 期刊id
	 */
	@GetMapping("remove")
	public ResponseResult<String> remove(Long journalId) {
		try {
			journalService.delete(journalId);
		} catch (Exception e) {
			logger.error("Exception=======>", e);
			return ResponseResult.fail(e.getMessage(), null);
		}
		return ResponseResult.success("删除成功, 该期刊对应的投稿记录已全部删除");
	}
}
